package com.cuitrip.config;

import com.cuitrip.mode.Commit;
import com.cuitrip.mode.Language;
import com.cuitrip.util.TextUtil;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MiJack on 2015/11/21.
 */
public class InputConfig {
    String fileName;
    String fileEncoding;
    List<Commit> commits;
    int key = -1;
    int oldKey = -1;
    int commit;
    Map<Integer, Language> map;
    private String isWindowFile;


    private InputConfig() {
        map = new HashMap<Integer, Language>();
        commits = new ArrayList<Commit>();
    }

    public String getFileName() {
        return fileName;
    }

    public String getFileEncoding() {
        return fileEncoding;
    }

    public List<Commit> getCommits() {
        return commits;
    }

    public int getKey() {
        return key;
    }

    public int getOldKey() {
        return oldKey;
    }

    public int getCommit() {
        return commit;
    }

    public Map<Integer, Language> getMap() {
        return map;
    }

    public String getIsWindowFile() {
        return isWindowFile;
    }

    public static InputConfig build(Element input) {
        InputConfig inputConfig = new InputConfig();
        Element csvFile = input.element("csv-file");
        inputConfig.fileName = csvFile.attributeValue("name");
        inputConfig.fileEncoding = csvFile.attributeValue("encoding");
        inputConfig.isWindowFile=csvFile.attributeValue("isWindowFile");
        Element commit = input.element("commit");
        inputConfig.commit = Integer.parseInt(commit.attributeValue("index"));
        for (Element element : (List<Element>) commit.elements("mode")) {
            inputConfig.commits.add(new Commit(element.attributeValue("mode"), element.attributeValue("value")));
        }
        Element key = input.element("key");
        if (key == null) {
            throw new RuntimeException("input 节点下缺少 key节点");
        }
        inputConfig.key = Integer.parseInt(key.attributeValue("index"));
        Element oldKey = input.element("old-key");
        if (key == null) {
            inputConfig.oldKey = -1;
        } else {
            inputConfig.oldKey = Integer.parseInt(oldKey.attributeValue("index"));
        }
        Element languages = input.element("languages");
        for (Element element : (List<Element>) languages.elements()) {
            Language language = new Language(
                    Integer.parseInt(element.attributeValue("index")),
                    element.attributeValue("name"),
                    element.attributeValue("short-name"),
                    element.attributeValue("prefix"));
            inputConfig.map.put(language.getIndex(), language);
        }
        return inputConfig;
    }

    @Override
    public String toString() {
        return "InputConfig\n{" +
                "fileName='" + fileName + '\'' +
                ", fileEncoding='" + fileEncoding + '\'' +
                ", commit='" + commit + '\'' +
                ", \ncommits=" + TextUtil.listToString(commits) +
                ",\nkey=" + key +
                ", oldKey=" + oldKey +
                ", \nmap=" + TextUtil.mapToString(map) +
                "\n}";
    }

}
